<?php

namespace App\Observers\Plane;

use App\Facades\AirportsGeoFacade;
use App\Models\Plane\Airports\Airport;
use Illuminate\Support\Facades\Cache;

class AirportsObserver
{
    /**
     * Handle the User "created" event.
     *
     * @param Airport $airport
     * @return void
     */
    public function created(Airport $airport)
    {
        $cache_key_code = "Airport:Code:{$airport->getCode()}";
        $cache_key_name = "Airport:Name:{$airport->getName()}";
        Cache::put($cache_key_name, $airport, 30 * config('trip.ttl.day'));
        Cache::put($cache_key_code, $airport, 30 * config('trip.ttl.day'));
        AirportsGeoFacade::addGeoByAirport($airport);
    }

    /**
     * Handle the User "updated" event.
     *
     * @param Airport $airport
     * @return void
     */
    public function updated(Airport $airport)
    {
        $cache_key_code = "Airport:Code:{$airport->getCode()}";
        $cache_key_name = "Airport:Name:{$airport->getName()}";
        Cache::put($cache_key_name, $airport, 30 * config('trip.ttl.day'));
        Cache::put($cache_key_code, $airport, 30 * config('trip.ttl.day'));
        AirportsGeoFacade::addGeoByAirport($airport);
    }

    public function deleted(Airport $airport)
    {
        $cache_key_code = "Airport:Code:{$airport->getCode()}";
        $cache_key_name = "Airport:Name:{$airport->getName()}";
        Cache::put($cache_key_name, $airport, 30 * config('trip.ttl.day'));
        Cache::forget($cache_key_code);
        AirportsGeoFacade::delGeo($airport->getCode());
    }
}
